package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 通用配置
 */
public class CommonDaoImpl extends TempDao {

	public Map<Integer, GoodsInfo> getGoodsInfoMapChannel(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_goods_channel where channelId = ?";
		Map<Integer, GoodsInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1,channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsInfo data = ResultSetParseEntityManger.initGoodsInfo(rs);
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, GoodsInfo> getGoodsInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_goods";
		Map<Integer, GoodsInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsInfo data = ResultSetParseEntityManger.initGoodsInfo(rs);
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, Map<Integer, List<GoodsBagInfo>>> getGoodsBagInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_goods_bag";
		Map<Integer, Map<Integer, List<GoodsBagInfo>>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsBagInfo data = new GoodsBagInfo();
				data.setId(rs.getInt("id"));
				data.setBagId(rs.getInt("boxId"));
				data.setGoodsId(rs.getInt("itemId"));
				data.setMinNum(rs.getInt("minNum"));
				data.setMaxNum(rs.getInt("maxNum"));
				data.setRandomType(rs.getInt("randomType"));
				data.setRandomWeight(rs.getInt("probability"));
				data.setNotice(rs.getInt("isNotice"));
				if(!dataMap.containsKey(data.getBagId())){
					dataMap.put(data.getBagId(), new ConcurrentHashMap<>());
				}
				if(!dataMap.get(data.getBagId()).containsKey(data.getRandomType())){
					dataMap.get(data.getBagId()).put(data.getRandomType(), new ArrayList<>());
				}
				dataMap.get(data.getBagId()).get(data.getRandomType()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, Map<Integer, List<GoodsBagInfo>>> getGoodsBagInfoMapChannel(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_goods_bag_channel where channelId = ?";
		Map<Integer, Map<Integer, List<GoodsBagInfo>>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1,channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsBagInfo data = new GoodsBagInfo();
				data.setId(rs.getInt("id"));
				data.setBagId(rs.getInt("boxId"));
				data.setGoodsId(rs.getInt("itemId"));
				data.setMinNum(rs.getInt("minNum"));
				data.setMaxNum(rs.getInt("maxNum"));
				data.setRandomType(rs.getInt("randomType"));
				data.setRandomWeight(rs.getInt("probability"));
				data.setNotice(rs.getInt("isNotice"));
				if(!dataMap.containsKey(data.getBagId())){
					dataMap.put(data.getBagId(), new ConcurrentHashMap<>());
				}
				if(!dataMap.get(data.getBagId()).containsKey(data.getRandomType())){
					dataMap.get(data.getBagId()).put(data.getRandomType(), new ArrayList<>());
				}
				dataMap.get(data.getBagId()).get(data.getRandomType()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
